package com.scau.hwadee.examination.controller;

import com.scau.hwadee.examination.entity.UserInfo;
import com.scau.hwadee.examination.service.LogInOutService;
import com.scau.hwadee.examination.vo.ResponseVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @author YHX
 * @date 2019/7/28  9:14
 * @description
 */
@Slf4j
@Controller
@RequestMapping("/account")
@Api(tags = "AccountsManagementController", description = "用户登录模块")
public class AccountsManagementController {
  @Autowired
  private LogInOutService logInOutService;

  private static Session getSession() {
    return SecurityUtils.getSubject().getSession();
  }

  @GetMapping("currentUserMessage")
  @ApiOperation(value = "获取当前用户的所有信息")
  public ResponseEntity<ResponseVo> getCurrentUserMessage() {
    ResponseVo vo = ResponseVo.instance();
    log.debug("获取当前用户的所有信息");
    Session session = getSession();
    vo.setData(session.getAttribute("userInfo"));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping(value = "/authcode")
  @ApiOperation(value = "获取验证码")
  public ResponseEntity<ResponseVo> getAuthCode(HttpServletResponse response) {
    Session session = getSession();
    logInOutService.getAuthCode(session, response);
    return new ResponseEntity<>(HttpStatus.OK);
  }

  @PostMapping(value = "/emailAuthCode")
  @ApiOperation(value = "获取邮箱验证码")
  public ResponseEntity<ResponseVo> email(@RequestBody Map<String, Object> map) {
    log.debug("获取邮箱验证码和发送邮件");
    log.debug("前端传过来的数据为：{}", map);
    ResponseVo vo = ResponseVo.instance();
    vo.setData(logInOutService.email(map));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping(value = "/register")
  @ApiOperation(value = "用户注册")
  @ApiImplicitParam(name = "userInfo", value = "用户信息", dataType = "UserInfo", required = true)
  public ResponseEntity<ResponseVo> register(@RequestBody UserInfo userInfo) {
    ResponseVo vo = ResponseVo.instance();
    vo.setMessage(logInOutService.register(userInfo));
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping(value = "/login")
  @ApiOperation(value = "用户登录")
  public ResponseEntity<ResponseVo> login(@RequestBody Map<String, Object> map) {
    Map<String, Object> m = logInOutService.login(map);
    ResponseVo vo = ResponseVo.instance();
    vo.setData(m);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @PostMapping(value = "/logout")
  @ApiOperation(value = "用户登出")
  public ResponseEntity<ResponseVo> logout() {
    String message = logInOutService.logout();
    ResponseVo vo = ResponseVo.instance();
    vo.setMessage(message);
    return new ResponseEntity<>(vo, HttpStatus.OK);
  }

  @GetMapping("index")
  public String redirectToIndex() {
    return "redirect:/index.html";
  }

}
